package com.lzy.huijinjiazu.mvp.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.di.component.DaggerBalaceDetailedComponent;
import com.lzy.huijinjiazu.di.module.BalaceDetailedModule;
import com.lzy.huijinjiazu.mvp.contract.BalaceDetailedContract;
import com.lzy.huijinjiazu.mvp.presenter.BalaceDetailedPresenter;
import com.lzy.huijinjiazu.mvp.ui.adapter.BalaceDetailedAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class BalaceDetailedFragment extends BaseFragment<BalaceDetailedPresenter> implements BalaceDetailedContract.View {

    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    private String mType;
    private BalaceDetailedAdapter mAdapter;

    private boolean isNoMore;

    public static BalaceDetailedFragment newInstance(String type) {
        BalaceDetailedFragment fragment = new BalaceDetailedFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerBalaceDetailedComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .balaceDetailedModule(new BalaceDetailedModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_balace_detailed, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mType = getArguments().getString("type");
        initRecyclerView();
        initData();
    }

    private void initRecyclerView() {
        ArmsUtils.configRecyclerView(mRvList, new LinearLayoutManager(getContext()));
        mAdapter = new BalaceDetailedAdapter();
        mRvList.setAdapter(mAdapter);
    }

    private void initData() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            data.add("");
        }
        mAdapter.setNewData(data);
    }

    public void onRefresh(RefreshLayout refreshlayout) {

    }

    public void onLoadmore(RefreshLayout refreshlayout) {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            data.add("");
        }
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                mAdapter.addData(data);
                if (mAdapter.getItemCount() > 60) {
                    Toast.makeText(getContext(), "数据全部加载完毕", Toast.LENGTH_SHORT).show();
                    refreshlayout.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
                    isNoMore = true;
                } else {
                    refreshlayout.finishLoadmore();
                }
            }
        }, 2000);
    }

    public boolean isNoMore() {
        return isNoMore;
    }
}
